#include <QDebug>

#include "gui/itemview.h"
#include "gui/shinylistitem.h"
#include "core/itemmodel.h"
#include "gui/itemviewadddialog.h"
#include "gui/itemvieweditdialog.h"
#include "gui/itemviewreportsdialog.h"
#include "core/itemcontroller.h"

void ItemView::onDataUpdate(QList<BaseModel *> models)
{
    ShinyListItem *listItem;
    ItemModel *item;
    QString mainText, subText;
    int i;

    qDebug() << "ItemView received update";
    BaseView::onDataUpdate(models);

    this->slist->clear();
    for (i = 0; i < models.length(); i++) {
        item = (ItemModel *) models.at(i);
        listItem = new ShinyListItem;
        listItem->setItemIcon("res/item.png");

        mainText = item->getName() + " ";
        mainText += item->getCode();
        subText = QString("%1 remaining; ").arg(item->getNumStocks());
        subText += "supplied by " + item->getSupplier()->getName();

        listItem->setMainText(mainText);
        listItem->setSubText(subText);

        this->slist->addItem(listItem);
    }
}

void ItemView::onDetailsRequested(BaseModel *model)
{
    int result;
    ItemModel *item = (ItemModel *) model;
    ItemController *itemCtrl = (ItemController *) this->getController();
    ItemViewDetailsDialog *dialog = new ItemViewDetailsDialog(this, item);
    dialog->setItem(item);
    result = dialog->exec();

    if (result == QDialog::Accepted) {
        ItemViewEditDialog *editDialog = new ItemViewEditDialog(this, item);
        editDialog->exec();

        item = editDialog->getItem();
        if (item) {
            itemCtrl->updateItem(item);
        }

        delete editDialog;
    }

    itemCtrl->getAll();  // Update the GUI, for when stocks get added or items get updated
    delete dialog;
}

QHBoxLayout *ItemView::createBottomComponents()
{
    buttonLayout = new QHBoxLayout;
    buttonLayout->addStretch();

    addButton = new QPushButton("Add Item to Inventory");
    buttonLayout->addWidget(addButton);

    buttonLayout->addStretch();

    return buttonLayout;
}

void ItemView::initViewComponents()
{
    BaseView::initViewComponents();

    topLabel->setText("Double-click an Item to see more details.");
}

void ItemView::onAddButtonClicked()
{
    ItemModel *item = new ItemModel;
    ItemController *itemCtrl = (ItemController *) this->getController();
    ItemViewAddDialog *dialog = new ItemViewAddDialog(this);

    dialog->exec();
    item = dialog->getItem();

    if (item) {
        itemCtrl->addItem(item);
        itemCtrl->getAll(); // Updates the UI
    }

    delete dialog;

}

void ItemView::onReportsButtonClicked()
{
    ItemViewReportsDialog *dialog = new ItemViewReportsDialog(this);
    dialog->exec();
    delete dialog;
}

void ItemView::initViewSignalsAndSlots()
{
    BaseView::initViewSignalsAndSlots();
    connect(this, SIGNAL(detailsRequested(BaseModel *)),
            this, SLOT(onDetailsRequested(BaseModel *)));
    connect(this->addButton, SIGNAL(clicked()),
            this, SLOT(onAddButtonClicked()));
    connect(this->reportsButton, SIGNAL(clicked()),
            this, SLOT(onReportsButtonClicked()));
}

ItemView::ItemView(QWidget *parent) :
    BaseView(parent)
{
    this->initViewComponents();
    this->initViewSignalsAndSlots();
}
